// GOAL_Library.cpp : implementation file
//

#include "stdafx.h"
#include "GOAL_Library_Dialog.h"
#include "Library_Functions.h"

  const unsigned int const_boxes[] = {
    GOAL_Library_Dialog::Char,
    GOAL_Library_Dialog::String,
    GOAL_Library_Dialog::Integer,
    GOAL_Library_Dialog::Short,
    GOAL_Library_Dialog::Long,
    GOAL_Library_Dialog::Float,
    GOAL_Library_Dialog::Double
  };

  const char * const_lookups[] = {
    "GOAL.Primitives.Char",
    "GOAL.Primitives.String",
    "GOAL.Primitives.Integer",
    "GOAL.Primitives.Short",
    "GOAL.Primitives.Long",
    "GOAL.Primitives.Float",
    "GOAL.Primitives.Double"
  };

// GOAL_Library dialog

IMPLEMENT_DYNAMIC(GOAL_Library_Dialog, CDialog)

GOAL_Library_Dialog::GOAL_Library_Dialog(BON::Folder& root,
                           CWnd* pParent /*=NULL*/)
	: CDialog(GOAL_Library_Dialog::IDD, pParent), root_ (root)
{
  for (UINT i = 0; i < NUM_BOXES; ++i)
    boxes_results_[i] = GOAL::objectExists (const_lookups[i], root_);

}

GOAL_Library_Dialog::~GOAL_Library_Dialog()
{
}

void GOAL_Library_Dialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(GOAL_Library_Dialog, CDialog)
  ON_BN_CLICKED(IDOK, &GOAL_Library_Dialog::OnBnClickedOk)
END_MESSAGE_MAP()

// GOAL_Library message handlers

BOOL GOAL_Library_Dialog::OnInitDialog()
{
  // TODO: Add your control notification handler code here

	CDialog::OnInitDialog();

  for (UINT i = 0; i < NUM_BOXES; ++i)
    CheckDlgButton (const_boxes[i], boxes_results_[i]);

  return TRUE;
}


// GOAL_Library message handlers

void GOAL_Library_Dialog::OnBnClickedOk()
{
  // TODO: Add your control notification handler code here

	CDialog::OnOK();
/*
  for (UINT i = 0; i < NUM_BOXES; ++i)
  {
    if (boxes_results_[i] != IsDlgButtonChecked (const_boxes[i]))
    {
      std::string message = const_lookups[i];

      // if they unchecked the box (boxes_results is the OLD state)
      if (boxes_results_[i])
        {
          message += ": Removing from project.";
          Udm::Object remove = GOAL::getObjectByPath (
            const_lookups[i],root_);

          remove.DeleteObject ();
        }
      // if they checked a box, then insert the library object
      else
        {
          message += ": Adding to the project.";
          Udm::Object proj_primitives = GOAL::getObjectByPath (
            GOAL::getParentPath (const_lookups[i]),root_);

          message += " Parent is ";
          message += GOAL::getParentPath (const_lookups[i]);

          // create the parent module if one isn't in the model
          if (proj_primitives == root_)
          {
            message += ": Creating parent. ";

            proj_primitives = GOAL::ModuleAdapter::Create (root_);

            GOAL::ModuleAdapter module = GOAL::ModuleAdapter::Cast (
              proj_primitives);

            module.name () = GOAL::getParentPath (const_lookups[i]);
            proj_primitives = module;
          }

          //AfxMessageBox ("Casting module adapter");
          // what I wanted to do was a deep copy, but that's not
          // the way any of this works.

          GOAL::ModuleAdapter parent = GOAL::ModuleAdapter::Cast (
              proj_primitives);

          //AfxMessageBox ("Casting class adapter");

          ::Udm::SmartDataNetwork library (GOAL::diagram);
          //Udm::SmartDataNetwork library2 (GOAL::diagram);
          GOAL::RootFolder library_root;
          
          GOAL::Library::read (GOAL::Library::Primitives, library);

          library_root = GOAL::RootFolder::Cast (library.GetRootObject ());

          std::set <GOAL::ModuleAdapter> modules = library_root.ModuleAdapter_kind_children ();

  for (std::set <GOAL::ModuleAdapter>::iterator j = modules.begin ();
        j != modules.end ();
        ++j)
        AfxMessageBox((*j).name ().operator std::string ().c_str ());


          GOAL::Class child = GOAL::Class::Cast (
            GOAL::getObjectByPath (const_lookups[i], library_root));
          
          AfxMessageBox ("Parent creation starting");

          child = child.Create (parent);
        }

      AfxMessageBox (message.c_str ());


    }
  }
*/
  
}
